<?php
/**
 * 日报表daily
 * String id
 * String content【内容】
 * String qa_ids【关联的需求或任务】
 * String add_time【发布日报的时间】
 * String add_user【谁发布的】
 */
class App_Model_Daily extends App_Model_Abstract
{
	public function init()
	{
		parent::init();
		$this->table = App_Db_Table::factory('Daily');
	}

	public function add($row)
	{
		return $this->table->insert($row);
	}

	public function delete($id)
	{
		$db = $this->table->getAdapter();
		$where = $db->quoteInto('id = ?', $id);
	
		return $this->table->delete($where);
	}

	public function getOne($id)
	{
		$row = $this->table->find($id);
		if ($row) {
			return $row->current();
		}
		return array();
	}
	
	public function update($row, $id)
	{
		$db = $this->table->getAdapter();
		$where = $db->quoteInto('id = ?', $id);
		
		return $this->table->update($row, $where);
	}
	
	public function getDaily($fieldArray)
	{
		$select = $this->table->select();
		if($fieldArray!=null){
			foreach ($fieldArray as $field => $value){
				$select->where("$field = ?",$value);
			}
		}
		$select->order('add_time desc');
		return $this->table->fetchAll($select);
	}
	
	public function listAll($title,$adduser,$starttime,$endtime,$page=1,$limit=20)
	{
		$select = $this->table->select();

		if(!empty($title)){
			$slice = explode(" ", trim($title));
			foreach($slice as $v){
				$select->where('title like ?','%'.$v.'%');
			}
		}
		if(!empty($adduser)){
			$select->where('add_user = ?',$adduser);
		}
		if(!empty($starttime)){
			$select->where('add_time > ?',$starttime);
		}
		if(!empty($endtime)){
			$select->where('add_time < ?',$endtime);
		}
		$offset = ($page - 1) * $limit;
		$select->limit($limit,$offset);
		
		$select->order('add_time desc');
        return $this->table->fetchAll($select);
	}
	
	public function getTotalNum($title,$adduser,$starttime,$endtime)
	{
		$where="where 1 ";

		if(!empty($title)){
			$arr = explode(' ', $title);//如果输入的title搜索有空格隔开，则分割字符串然后再生成语句
			foreach ($arr as $v){
				$where.=" and title like '%$v%'";
			}
		}
		if(!empty($adduser)){
			$where.=" and add_user='$adduser'";
		}
		if(!empty($starttime)){
			$where.=" and add_time>='".$starttime."'";
		}
		if(!empty($endtime)){
			$where.=" and add_time<='".$endtime."'";
		}
		return $this->table->getAdapter()->fetchOne("select count(*) from daily ".$where); 
	}
}